/*
 * FeedExtractor.java
 *
 * Created on August 2, 2007, 9:09 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.afaker.rss.feed.biz;

import com.afaker.rss.common.GeneralUtils;
import com.afaker.rss.context.ApplicationContext;
import com.afaker.rss.search.Indexer;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.fetcher.FetcherException;
import com.sun.syndication.fetcher.impl.HttpClientFeedFetcher;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.SyndFeedOutput;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import org.xml.sax.InputSource;
import com.afaker.rss.feed.EntrySet;
import com.afaker.rss.feed.ROMEFeedEntryImpl;

/**
 *
 * @author bruce
 */
public class FeedExtractor {

    private String uri;
    private Indexer indexer;

    /** Creates a new instance of FeedExtractor */
    public FeedExtractor(String uri) {
        this.uri = uri;
    }

    public EntrySet extract() throws MalformedURLException, IOException, FeedException, FetcherException {
        HttpClientFeedFetcher fetcher = new HttpClientFeedFetcher();
        URL url = new URL(uri);
        SyndFeed newFeed = fetcher.retrieveFeed(url);
        String title = newFeed.getTitle();
        SyndFeedInput input = new SyndFeedInput();
        InputSource source;
        File feedFile = new File(GeneralUtils.getLocalChannelName(title, true, true));
        if (!feedFile.exists()) {
            return null;
        }
        SyndFeed originalFeed = null;
        originalFeed = input.build(feedFile);
        Iterator it1 = originalFeed.getEntries().listIterator();
        ArrayList<String> titles = new ArrayList<String>();
        while (it1.hasNext()) {
            titles.add(((SyndEntry) it1.next()).getTitle());
        }
        Iterator it2 = newFeed.getEntries().listIterator();
        SyndEntry temp;
        ArrayList<String> list = new ArrayList<String>();
        EntrySet newEntry = new EntrySet();
        while (it2.hasNext()) {
            temp = (SyndEntry) it2.next();
            if (titles.contains(temp.getTitle())) {
                list.add(temp.getTitle());
            } else {
                newEntry.addEntry(new ROMEFeedEntryImpl(temp, newFeed.getTitle()));
            }
        }
        it1 = originalFeed.getEntries().listIterator();
        ArrayList entries = new ArrayList();
        while (it1.hasNext()) {
            temp = (SyndEntry) it1.next();
            if (list.contains(temp.getTitle())) {
                it1.remove();
            } else {
                entries.add(temp);
            }
        }
        originalFeed.setEntries(entries);
        SyndFeedOutput output = new SyndFeedOutput();
        output.output(HttpClientFeedFetcher.combineFeeds(originalFeed, newFeed),
                new File(GeneralUtils.getLocalChannelName(originalFeed.getTitle(), true, true)));
        if (indexer == null) {
            indexer = new Indexer();
        } else {
            indexer.index(GeneralUtils.getLocalChannelName(originalFeed.getTitle() ,true,true));
        }
        System.out.println(ApplicationContext.getApplicationContext().getProperty("channel.update.success") +
                originalFeed.getTitle());
        return newEntry;
    }

    /**Test*/
    public static void main(String args[]) throws MalformedURLException {
        FeedExtractor extractor = new FeedExtractor("http://127.0.0.1/wordpress/?feed=rss2");
        try {
            extractor.extract();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (FetcherException ex) {
            ex.printStackTrace();
        } catch (FeedException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}